package com.pets.springboot_petshop.mapper;

import com.pets.springboot_petshop.entity.Service;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author: wxk
 * @Email: 2089967794@qq.com
 * @Date: 2024/10/14 上午11:46 周一
 * @Description: 服务数据访问层接口
 */
@Mapper
@Repository
public interface ServiceMapper {
    //id查询
    @Select("SELECT a.id, a.name , a.description, a.original_price, a.discount_price, a.pettype_id, b.name AS pettype_name, a.img, a.create_time " +
            "FROM service a " +
            "LEFT JOIN pettype b ON a.pettype_id = b.id " +
            "WHERE a.id = #{id};")
    Service selectService(int id);
    //pettype_id，通过宠物分类查询
    @Select("SELECT a.id, a.name, a.description, a.original_price, b.name AS pettype_name, a.discount_price, a.pettype_id, a.img, a.create_time " +
            "FROM service a " +
            "LEFT JOIN pettype b ON a.pettype_id = b.id " +
            "WHERE b.name = #{pettype_name}")
    List<Service> selectPettypeName(String pettype_name);
    //name，通过姓名模糊查询
    @Select("select a.id,a.name,a.description,a.original_price,a.discount_price,a.pettype_id,b.name AS pettype_name,a.img,a.create_time from service a LEFT JOIN pettype b on a.pettype_id = b.id where a.name like concat('%',#{name},'%');")
    List<Service> selectServicename(String name);
    //name,通过姓名查询
    @Select("select a.id,a.name,a.description,a.original_price,a.discount_price,a.pettype_id,b.name AS pettype_name,a.img,a.create_time from service a LEFT JOIN pettype b on a.pettype_id = b.id where a.name = #{name};")
    Service selectname(String name);
    //查询所有
    @Select("select a.id,a.name,a.description,a.original_price,a.discount_price,a.pettype_id,b.name AS pettype_name,a.img,a.create_time,a.update_time from service a LEFT JOIN pettype b on a.pettype_id = b.id;")
    List<Service> findAll();
    //添加
    @Insert("insert into service(name,description,original_price,discount_price,pettype_id,img,create_time,update_time)values(#{name},#{description},#{original_price},#{discount_price},#{pettype_id},#{img},now(),now());")
    int addService(Service service);
    //删除
    @Delete("delete from service where id=#{id};")
    int deleteServiceById(int id);
    //修改数据
    @Update("UPDATE service SET name=#{name},description=#{description},original_price=#{original_price},discount_price=#{discount_price},pettype_id=#{pettype_id},img=#{img},update_time=now() where id= #{id};")
    int updateServiceById(Service service);
}